<template>
  <el-card class="menu-container">
    <template #header>
      <div class="header">
        <el-button type="primary" size="small" icon="el-icon-plus" @click="handleAdd">增加</el-button>
        <el-popconfirm
            title="确定删除吗？"
            @confirm="handleDelete"
        >
          <template #reference>
            <el-button type="danger" size="small" icon="el-icon-delete">批量删除</el-button>
          </template>
        </el-popconfirm>
      </div>
    </template>
    <el-table
        v-loading="loading"
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange">
      <el-table-column
          type="selection"
          width="55"
      >
      </el-table-column>
      <el-table-column
          prop="name"
          label="菜单名称"
          width="400"
      >
      </el-table-column>
      <el-table-column
          prop="url"
          label="菜单url"
          width="620"
      >
      </el-table-column>
      <el-table-column
          label="操作"
      >
        <template #default="scope">
          <a style="cursor: pointer; margin-right: 10px" @click="handleEdit(scope.row.id)">修改</a>
          <a style="cursor: pointer; margin-right: 10px" @click="handleAddRole(scope.row.id)">分配角色</a>
          <el-popconfirm
              title="确定删除吗？"
              @confirm="handleDeleteOne(scope.row.id)"
          >
            <template #reference>
              <a style="cursor: pointer">删除</a>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!--总数超过一页，再展示分页器-->
    <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        :page-size="pageSize"
        :current-page="currentPage"
        @current-change="changePage"
    />
  </el-card>
  <DialogAddMenu ref='addGood' :reload="getMenu" :type="type" :level="level" :parent_id="parent_id"/>
  <RoleChoose ref="roleChoose"/>
</template>

<script>
import {onMounted, onUnmounted, reactive, ref, toRefs} from 'vue'
import {useRoute, useRouter} from 'vue-router'
import {ElMessage} from 'element-plus'
import axios from '@/utils/axios'
import DialogAddMenu from "@/components/DialogAddMenu.vue";
import RoleChoose from "@/components/RoleChoose.vue";

export default {
  name: 'menu',
  components: {
    RoleChoose,
    DialogAddMenu
  },
  setup() {
    const multipleTable = ref(null)
    const addGood = ref(null)
    const roleChoose = ref(null)
    const router = useRouter()
    const route = useRoute()
    const state = reactive({
      loading: false,
      tableData: [], // 数据列表
      multipleSelection: [], // 选中项
      total: 0, // 总条数
      currentPage: 1, // 当前页
      pageSize: 10, // 分页大小
      type: 'add', // 操作类型
      level: 1,
      parent_id: 0
    })
    onMounted(() => {
      getMenu()
    })
    onUnmounted(() => {
      unwatch()
    })
    const unwatch = router.afterEach((to) => {
      if (['Menu', 'level2', 'level3'].includes(to.name)) {
        getMenu()
      }
    })
    // 获取分类列表
    const getMenu = () => {
      state.loading = true
      axios.get('/evaluate/menu/page', {
        params: {
          current: state.currentPage,
          size: state.pageSize,
          name: ''
        }
      }).then(res => {
        state.tableData = res.records
        state.total = res.total
        state.currentPage = res.current
        state.loading = false
      })
    }
    // 添加分类
    const handleAdd = () => {
      state.type = 'add'
      addGood.value.open()
    }
    // 修改分类
    const handleEdit = (id) => {
      state.type = 'edit'
      addGood.value.open(id)
    }

    const handleAddRole = (id) => {
      roleChoose.value.open(id)
    }
    // 选择项
    const handleSelectionChange = (val) => {
      state.multipleSelection = val
    }
    // 批量删除
    const handleDelete = () => {
      if (!state.multipleSelection.length) {
        ElMessage.error('请选择项')
        return
      }
      axios.delete('/evaluate/menu', {
        data: state.multipleSelection.map(i => i.id)
      }).then(() => {
        ElMessage.success('删除成功')
        getMenu()
      })
    }
    // 单个删除
    const handleDeleteOne = (id) => {
      axios.delete('/evaluate/menu', {
        data: [id]
      }).then(() => {
        ElMessage.success('删除成功')
        getMenu()
      })
    }
    const changePage = (val) => {
      state.currentPage = val
      getMenu()
    }
    return {
      ...toRefs(state),
      multipleTable,
      handleSelectionChange,
      addGood,
      roleChoose,
      handleAdd,
      handleAddRole,
      handleEdit,
      handleDelete,
      handleDeleteOne,
      getMenu,
      changePage
    }
  }
}
</script>

<style scoped>
.menu-container {
  min-height: 100%;
}

.el-card.is-always-shadow {
  min-height: 100% !important;
}
</style>